﻿using LJ.AspNetCore.Abstractions.Pack;
using LJ.Socket.Server.StraightChicken.Internal;
using LJ.Socket.Server.StraightChicken.Pack;

namespace LJ.Socket.Server.StraightChicken.SignalR.Pack;

public class SocketServerStraightChickenSignalRPackBase : AspNetCorePackBase, ISocketServerStraightChickenPack
{
    public override void Add(IAspNetCorePackContext aspNetCorePackContext)
    {
        aspNetCorePackContext.DependencyManager.ServiceCollection.AddSignalR(options =>
        {
            options.EnableDetailedErrors = true;
            options.HandshakeTimeout = TimeSpan.FromSeconds(15);
            //服务器端向客户端 ping的间隔
            options.KeepAliveInterval = TimeSpan.FromSeconds(15);
            //要求30s内必须收到客户端发的一条消息，如果没有收到，那么服务器端则认为客户端掉了
            options.ClientTimeoutInterval = TimeSpan.FromSeconds(30);
        });

        var socketServerHandlerManager = new SocketServerHandlerManager(aspNetCorePackContext.DependencyManager, aspNetCorePackContext.ReflectionManager);
        aspNetCorePackContext.DependencyManager.ServiceCollection.AddSingleton<ISocketServerHandlerManager>(socketServerHandlerManager);


        aspNetCorePackContext.DependencyManager.ServiceCollection.AddSingleton<ISocketMessageListener, SocketMessageListener>();
        aspNetCorePackContext.DependencyManager.ServiceCollection.AddSingleton<ISocketServerHandlerInvoker, SocketServerHandlerInvoker>();
        aspNetCorePackContext.DependencyManager.ServiceCollection.AddSingleton<ISocketServerHandlerInvoker, SocketServerHandlerInvoker>();

    }

    public override void Use(IAspNetCorePackProvider aspNetCorePackProvider)
    {
        aspNetCorePackProvider.EndpointRouteBuilder.MapHub<StraightChickenHub>($"/StraightChicken");
    }
}
